package com.stadium.common.utils;

import com.stadium.entity.SysUser;
import com.stadium.vo.GamePlayerVo;
import com.stadium.vo.PlayerUserVo;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

/**
 * @Description:
 * @Author WeiShunli
 * @Date 2021/5/6 22:07
 */
public class ExcelToObj {

    public static List<GamePlayerVo> excelToUser(String suffix, InputStream is) throws IOException {
        List<GamePlayerVo> list = new ArrayList<>();
        Workbook xssfWorkbook = null;
        if (".xls".equals(suffix)) {
            xssfWorkbook = new HSSFWorkbook(is);
        } else if (".xlsx".equals(suffix)) {
            xssfWorkbook = new XSSFWorkbook(is);
        }
        //获取第一张表单的信息
        Sheet sheet = xssfWorkbook.getSheetAt(0);
        //如果第一张表单为null,直接返回空
        if (sheet == null) {
            return null;
        }
        //循环行
        for (int i = 0; i <= sheet.getLastRowNum(); i++) {
            Row row = sheet.getRow(i);
            //如果是表头,判断表头信息
            if (row.getRowNum() == 0) {
                continue;
            }
            //1.封装到实体类中
            GamePlayerVo SysUser=new GamePlayerVo();
            //2.名字
            String name = ExcelUtils.getValue(row.getCell(0));
            SysUser.setName(name);
            //身份证号
            String id = ExcelUtils.getValue(row.getCell(1));
            SysUser.setIdNumber(id);
            //3.电话
            String number = ExcelUtils.getValue(row.getCell(2));
            SysUser.setPhoneNum(number);
            //性别
            String sex = ExcelUtils.getValue(row.getCell(3));
            if("女".equals(sex)){
                SysUser.setSex("1");
            }else if("男".equals(sex)){
                SysUser.setSex("2");
            }else {
                SysUser.setSex("0");
            }
            //3.身高
            String hight = ExcelUtils.getValue(row.getCell(4));
            SysUser.setHeight(Integer.parseInt(hight));
            //3.体重
            String weight = ExcelUtils.getValue(row.getCell(5));
            SysUser.setWeight(Integer.parseInt(weight));
            //3.地区
            String area = ExcelUtils.getValue(row.getCell(6));
            SysUser.setArea(area);
            //3.球龄
            String ballAge = ExcelUtils.getValue(row.getCell(7));
            SysUser.setBallAge(Integer.parseInt(ballAge));
            //球衣号
            String jerseyNum = ExcelUtils.getValue(row.getCell(8));
            SysUser.setJerseyNum(Integer.parseInt(jerseyNum));
            list.add(SysUser);
        }
        return list;
    }
}
